package com.young.monitor.service.impl;

import com.young.common.exception.BaseException;
import com.young.common.util.CommonUtil;
import com.young.common.core.dal.service.IDataAccessService;
import com.young.common.util.DateUtil;
import com.young.common.util.Page;
import com.young.common.util.StringUtils;
import com.young.interfaces.log.annotation.Log;
import com.young.monitor.model.ApiTrace;
import com.young.monitor.service.IApiTraceService;
import org.springframework.stereotype.Service;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
* 链路追踪服务实现
* Created by imrookie on 2019-3-10.
*/
@Service("apiTraceService")
public class ApiTraceServiceImpl implements IApiTraceService {

    @Resource(name = "dataAccessService")
    IDataAccessService dataAccessService;//数据层服务

    private static final Logger logger = LoggerFactory.getLogger(ApiTraceServiceImpl.class);

    private static final String MAPPER = "com.young.monitor.mapper.apiTrace";//mapper的namespace

    //获取数据列表
    @Override
    public List<ApiTrace> getList(ApiTrace obj) {
        return dataAccessService.getList(MAPPER + ".getList", obj);
    }

    @Override
    public List<ApiTrace> getListByTraceId(String traceId) {
        if (StringUtils.isBlank(traceId)) {
            throw new BaseException("链路ID不允许为空");
        }
        ApiTrace apiTrace = new ApiTrace();
        apiTrace.setTraceId(traceId);
        return this.getList(apiTrace);
    }

    @Override
    public List<ApiTrace> getListByBusinessId(String businessId) {
        if (StringUtils.isBlank(businessId)) {
            throw new BaseException("业务ID不允许为空");
        }
        ApiTrace apiTrace = new ApiTrace();
        apiTrace.setBusinessId(businessId);
        return this.getList(apiTrace);
    }

    //获取数据列表(分页)
    @Override
    public List<ApiTrace> getPage(ApiTrace obj, Page page) {
        return dataAccessService.getList(MAPPER + ".getList", obj, page);
    }

    //查询单条
    @Override
    public ApiTrace get(String id) {
        return dataAccessService.getObject(MAPPER + ".get", id);
    }

    //统计数量
    @Override
    public int count(ApiTrace obj) {
        return dataAccessService.getObject(MAPPER + ".count", obj);
    }

    //修改
    @Override
    public int update(ApiTrace obj) {
        return dataAccessService.update(MAPPER + ".update", obj);
    }

    //批量删除
    @Override
    public int delete(String[] idArr) {
        int i = 0;
        for(String id : idArr){
            i += this.delete(id);
        }
        return i;
    }

    //删除单条
    @Override
    public int delete(String id) {
        return dataAccessService.delete(MAPPER + ".delete", id);
    }

    //插入
    @Override
    public int insert(ApiTrace obj) {
        if (StringUtils.isBlank(obj.getId())) {
            obj.setId(CommonUtil.getUUID());
        }
        if (StringUtils.isNotBlank(obj.getOutParam()) && obj.getOutParam().length() > 2000) {
            obj.setOutParam(obj.getOutParam().substring(0, 2000));
        }
        if (StringUtils.isNotBlank(obj.getInParam()) && obj.getInParam().length() > 1000) {
            obj.setInParam(obj.getInParam().substring(0, 1000));
        }
        if (StringUtils.isNotBlank(obj.getEx()) && obj.getEx().length() > 1000) {
            obj.setEx(obj.getEx().substring(0, 1000));
        }
        if (StringUtils.isNotBlank(obj.getExtParam()) && obj.getExtParam().length() > 500) {
            obj.setExtParam(obj.getExtParam().substring(0, 500));
        }
        if (StringUtils.isNotBlank(obj.getBrowser()) && obj.getBrowser().length() > 128) {
            obj.setBrowser(obj.getBrowser().substring(0, 128));
        }
        if (StringUtils.isNotBlank(obj.getOs()) && obj.getOs().length() > 128) {
            obj.setOs(obj.getOs().substring(0, 128));
        }
        if (StringUtils.isNotBlank(obj.getUri()) && obj.getUri().length() > 128) {
            obj.setUri(obj.getUri().substring(0, 128));
        }
        if (StringUtils.isNotBlank(obj.getUrl()) && obj.getUrl().length() > 256) {
            obj.setUrl(obj.getUrl().substring(0, 256));
        }
        if (StringUtils.isNotBlank(obj.getReferer()) && obj.getReferer().length() > 256) {
            obj.setReferer(obj.getReferer().substring(0, 256));
        }
        if (StringUtils.isNotBlank(obj.getOrigin()) && obj.getOrigin().length() > 128) {
            obj.setOrigin(obj.getOrigin().substring(0, 128));
        }
        return dataAccessService.insert(MAPPER + ".insert", obj);
    }

    //批量插入
    @Override
    public int batchInsert(List<ApiTrace> list) {
        int i = 0;
        for(ApiTrace item : list){
            i += this.insert(item);
        }
        return i;
    }

    /**
     * 清空所有数据
     */
    @Log("清空所有链路数据")
    @Override
    public void clearAllData() {
        dataAccessService.update(MAPPER + ".truncate", null);
        logger.warn("[清空所有链路数据] 清空完毕");
    }

    /**
     * 清空指定时间之前的数据
     *
     * @param date 删除的时间点
     */
    @Log("按时间删除链路数据")
    @Override
    public int deleteDataBefore(Date date) {
        int num = dataAccessService.update(MAPPER + ".deleteDataBefore", date);
        logger.warn("[按时间删除链路数据] 时间={},删除数据条数={}", DateUtil.dateFormat(date), num);
        return num;
    }

}